package com.hejjon.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hejjon.core.domain.entity.RecordRelTableInfo;
import com.hejjon.core.domain.vo.RecordRelTableInfoVO;
import com.hejjon.core.enumer.DeleteFlagEnum;
import com.hejjon.core.utils.StrUtil;
import com.hejjon.domain.dto.RecordRelTableInfoDTO;
import com.hejjon.mapper.RecordRelTableInfoMapper;
import com.hejjon.service.IRecordRelTableInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RecordRelTableInfoServiceImpl extends ServiceImpl<RecordRelTableInfoMapper, RecordRelTableInfo> implements IRecordRelTableInfoService {

    @Resource
    private RecordRelTableInfoMapper recordRelTableInfoMapper;

    @Override
    public boolean batchSave(List<RecordRelTableInfoDTO> dtoList) {

        List<RecordRelTableInfo> entityList = new ArrayList<>();
        for (RecordRelTableInfoDTO infoDTO : dtoList) {
            RecordRelTableInfo info = new RecordRelTableInfo();
            BeanUtils.copyProperties(infoDTO, info);
            Date date = new Date();
            info.setId(StrUtil.getUUID());
            info.setCreateTime(date);
            info.setCreater("admin");
            info.setDeleteFlag(DeleteFlagEnum.NO.getCode());
            entityList.add(info);
        }
        return recordRelTableInfoMapper.batchInsert(entityList) > 0;
    }


    @Override
    public boolean batchUpdate(List<RecordRelTableInfoDTO> dtoList) {

        List<RecordRelTableInfo> entityList = new ArrayList<>();
        for (RecordRelTableInfoDTO infoDTO : dtoList) {
            RecordRelTableInfo info = new RecordRelTableInfo();
            info.setRecordId(infoDTO.getRecordId());
            info.setFeatureId(infoDTO.getFeatureId());
            info.setTableName(infoDTO.getTableName());
            info.setDatabaseId(infoDTO.getDatabaseId());
            info.setDatabaseName(infoDTO.getDatabaseName());
            info.setUpdaterId("admin");
            info.setUpdater("admin");
            entityList.add(info);
        }
        return recordRelTableInfoMapper.batchUpdate(entityList) > 0;
    }

    @Override
    public int batchUpdateDatabase(List<RecordRelTableInfo> infoList) {
        return recordRelTableInfoMapper.batchUpdateDatabase(infoList);
    }


    @Override
    public List<RecordRelTableInfoVO> listByRecordIds(List<String> recordIds) {
        return recordRelTableInfoMapper.selectByRecordIds(recordIds);
    }

    @Override
    public boolean removeByRecordId(String recordId) {
        LambdaUpdateWrapper<RecordRelTableInfo> wrapper = new LambdaUpdateWrapper<RecordRelTableInfo>().eq(RecordRelTableInfo::getDeleteFlag, DeleteFlagEnum.NO.getCode())
                .eq(StringUtils.isNotEmpty(recordId), RecordRelTableInfo::getRecordId, recordId)
                .set(RecordRelTableInfo::getUpdateTime, LocalDateTime.now()).set(RecordRelTableInfo::getUpdaterId, "admin").set(RecordRelTableInfo::getDeleteFlag, DeleteFlagEnum.YES.getCode());

        return update(wrapper);
    }

    @Override
    public List<RecordRelTableInfoVO> listByRecordId(String recordId) {
        return recordRelTableInfoMapper.selectByRecordId(recordId);
    }

    @Override
    public RecordRelTableInfoVO getByRecordIdAndFeatureId(String recordId, String featureId) {
        return recordRelTableInfoMapper.selectByRecordIdAndFeatureId(recordId, featureId);
    }
}
